package com.shuoyue.ycgk.ui.questionbank.practice.paperset.questionitem;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.ui.questionbank.UserPaperQuestionContract;
import com.shuoyue.ycgk.ui.questionbank.base.BaseQuestionActivity;
import com.shuoyue.ycgk.utils.TimeFomatUtil;
import com.shuoyue.ycgk.views.dialog.AnswerCardDialog;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.List;
import java.util.TimerTask;

/**
 * \
 * 考题
 */
public class DoPaperActivityBack extends BaseQuestionActivity implements UserPaperQuestionContract.View {


    long useTimeMul;//已用时毫秒数
    UserPaperQuestionContract.Presenter spePresenter;
    UserTestPaper userpaper;

    @Override
    protected void initView() {
        super.initView();
        card.setVisibility(View.VISIBLE);
        time.setVisibility(View.VISIBLE);
    }


    public static void start(Context source,
                             UserTestPaper pager) {
        source.startActivity(new Intent(source, DoPaperActivityBack.class)
                .putExtra("pager", pager)
        );
    }

    @Override
    public Class getClassT() {
        return DoPaperMateralFragment.class;
    }

    @Override
    public Class getClassK() {
        return DoPaperSimpleFragment.class;
    }

    @Override
    public void initData() {
        super.initData();
        userpaper = (UserTestPaper) getIntent().getSerializableExtra("pager");
        spePresenter = new UserPaperQuestionContract.Presenter();
        spePresenter.attachView(this);
        spePresenter.getQuestions(userpaper.getMemberTestPaperId(),3, userpaper.getType());
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        BaseMvpFragment fragment = questionFragmentAdapter.getCurrentFragment();
        if (fragment instanceof DoPaperMateralFragment) {
            ((DoPaperMateralFragment) fragment).gesture.onTouchEvent(event);
        }
        super.dispatchTouchEvent(event);
        return false;
    }

    @Override
    public void lastOne() {
        clickAnswerCard();
    }

    @Override
    public void back() {
        HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                "提示",
                "是否退出");
        hintConfirmDialog.setOkListener(v -> finish());
        hintConfirmDialog.show();
    }



    final int TIMER_RSPREAD = 500;//计时器间隔
    Handler handler = new Handler();
    TimerTask clock = new TimerTask() {
        @Override
        public void run() {
            handler.postDelayed(clock, TIMER_RSPREAD);
            //TODO 计时
            useTimeMul += TIMER_RSPREAD;
            runOnUiThread(() -> {
                String useTime = TimeFomatUtil.formatmmss(useTimeMul / 1000);
                time.setText(useTime);
                if (answerCardDialog != null) {
                    answerCardDialog.setUseTime(useTime);
                }
            });
        }
    };

    AnswerCardDialog answerCardDialog;

    void clickAnswerCard() {
        if (answerCardDialog == null) {
            answerCardDialog = new AnswerCardDialog(mContext, questionParents);
            answerCardDialog.addSelectCall(new AnswerCardDialog.AnswerCardCallBack() {
                @Override
                public void select(QuestionSimple questionSimple) {
                    //TODO 跳转到指定位置
                    goDesPosition(questionSimple);
                }

                @Override
                public void submit() {
                    //TODO  提交答案
                }
            });
        }
        answerCardDialog.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        handler.postDelayed(clock, TIMER_RSPREAD);
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(clock);
    }

    @Override
    public void finish() {
        //TODO 结束前提交信息
        super.finish();
    }

    @Override
    public void setQuestions(List<QuestionParent> specialPager) {
        setThisActData(specialPager);
    }
}
